/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scclient03;

import java.awt.Dimension;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author laoxu
 */
public class SCClient03 {
    private DataInputStream dis;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
       SCClient03 scc = new SCClient03();
       if(!scc.connect("10.3.115.170", 10000)){
           return;
       }
       scc.showClientUI();
    }
    public boolean connect(String host,int port){
        boolean fag = true ;
        try {
            Socket sock = new Socket(host,port);
            dis = new DataInputStream(sock.getInputStream());
        } catch (UnknownHostException ex) {
            fag = false;
        } catch (IOException ex) {
            fag = false;
        }
        return fag;
    }
    public void showClientUI() throws IOException{
      ClientUI clientUI = new ClientUI(dis);
      clientUI.updateSize(readServerSize());
      int i = 0;
      while(true){
          byte[] imageData = readImageData();
//          File file = new File("f:\\image\\" + i + ".jpg");
//          FileOutputStream fos = new FileOutputStream(file);
//          fos.write(imageData, 0, imageData.length);
//          i++;
          clientUI.upDate(imageData);
      }
    }
    public byte[] readImageData() throws IOException {
        int len = dis.readInt();
        System.out.println(len + "");
        byte[] data = new byte[len];
        dis.readFully(data,0,len);        
        return data;
    }
    public  Dimension readServerSize(){
        double width = 0,height = 0;
        try {            
            width = dis.readDouble();
            height = dis.readDouble();          
        } catch (IOException ex) {
            Logger.getLogger(SCClient03.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new Dimension((int)width,(int)height);
    }
}
